/**
 * 房間玩家,坐下按鈕视图控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-8 13:32
 */
package com.addcn.texas.view
{
	import flash.display.*;
	import flash.events.Event;
	import flash.display.Stage;		
	import flash.events.MouseEvent;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;
	import com.addcn.texas.utils.GetPhoto;
	import com.addcn.texas.utils.Coordinate;
	import com.addcn.common.utils.DestroyMemory;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
		
	public class RoomUserView extends View
	{				
		public static const HIDEN_USER_STATUS : String = "hideUserStatus";
		
		protected var _container : Object;
		private var _app : AppLibary = AppLibary.getInstance();	
		private var _class : Class;
		private var _components : Object;
		private var _sit_button : Array;  //坐下按鈕				
		private var _my_sit : int;        //當前玩家自己的座位號
		private var _max_bet : int;		  //當前允許下注最大數
		private var _my_chip : int;		  //玩家自己的籌碼數
		
		public function RoomUserView(r_target : Object) {
			this._container = new Sprite;			
			r_target.addChild(_container);		
			_intialize();
		}			
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [
								RoomUserModel.USER_PHOTO_MODIFY,								
								RoomUserModel.ROOM_USER_DATA_REDY,								
								RoomUserModel.USER_SITDOWN,
								RoomUserModel.USER_STANDUP,									
								//RoomUserModel.MY_SIT_NUM,
								RoomUserModel.SITDOWN_ERR,	
								RoomUserModel.UPDATE_USER_CHIP,
								GameInfoModel.GAME_START,
								GameInfoModel.USER_STATUS_MSG,								
								GameInfoModel.GAME_WIN_RESULT,																							
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {
				case RoomUserModel.USER_PHOTO_MODIFY:
					_setPhoto(r_data);
					break;
				case RoomUserModel.ROOM_USER_DATA_REDY:	
				case RoomUserModel.USER_SITDOWN:								
					_setUserSit(r_data);
					break;
				case RoomUserModel.USER_STANDUP:
					_clearUserSit(r_data);
					break;		
				//case RoomUserModel.MY_SIT_NUM:
					//this._my_sit = r_data["sit"];
					//break;	
				case RoomUserModel.SITDOWN_ERR:
					sendNotification(PopupView.SHOW_ALERT_BOX, r_data);
					break;	
				case RoomUserModel.UPDATE_USER_CHIP:
					_modifyUserInfo(r_data);
					break;
				case GameInfoModel.GAME_START:
					this._hideUserInfo();	
					break;
				case GameInfoModel.USER_STATUS_MSG:			
					_showUserStatus(r_data["sit"], r_data["msg"]);					
					break;
				case GameInfoModel.GAME_WIN_RESULT:
					_showGameResult(r_data);
					break;
			}			
		}
		
		private function _intialize() : void
		{						
			_my_sit   = 0;			
			_components = { };				
			_sit_button = Coordinate.getInstance().SitCoord();	//获取所有坐下按钮坐标					
			for (var _key in _sit_button)  {//初始化所有坐下按鈕
				_addBuntton(_key);
			}
		}
		
		//添加坐下按鈕
		private function _addBuntton(r_sit: int) : void
		{
			var _button : Object;
			var _class = this._app.getClass("SitButton");
			if (_class == null) return;	
			_button = new _class;				
			_container.addChild(_button);							
			_components["SitButton" + r_sit] = _button;				
			_button.name = r_sit;
			_button.x = _sit_button[r_sit].x;
			_button.y = _sit_button[r_sit].y;
			_button.addEventListener(MouseEvent.CLICK, sitDownHanlder);
		}
		
		//移除坐下按鈕
		private function _removeButton(r_sit : int) : void
		{
			var _btn = _components["SitButton" + String(r_sit)];
			if (_btn != null) {
				_btn.removeEventListener(MouseEvent.CLICK, sitDownHanlder);
				_btn.parent.removeChild(_btn);
				_components["SitButton" + String(r_sit)] = null;
			}
		}
		
		//添加玩家信息框
		private function _addUserBox(r_data : Object) : void
		{			
			var _sit = r_data["sit"];
			if (_sit <1 || _sit > 9) {
				trace("##not allow sit:" + _sit);
				return;
			}			
			var _class = _app.getClass("com.addcn.texas.view.components.UserInfoBox");
			if (_class == null) return ;	
			var _box  = new _class;
			this._container.addChild(_box);
			_components["UserInfoBox" + _sit] = _box;			
			_box.x = this._sit_button[_sit].x;
			_box.y = this._sit_button[_sit].y;	
			_box.userId = r_data["id"];
			_modifyUserInfo(r_data);
		}
		
		/**
		 * 修改玩家显示的信息
		 * @param	r_data
		 */
		private function _modifyUserInfo(r_data : Object) : void
		{						
			MonsterDebugger.trace("modify ", r_data)
			if (!r_data["sit"]) return;
			var _box : Object = _components["UserInfoBox" + r_data["sit"]];
			if (!_box) return;
			if(r_data["name"]){
				_box.userName = r_data["name"];
			}
			if(r_data["chip"] != null){
				_box.userMoney = String(r_data["chip"]);
			}
			this._my_chip = r_data["chip"];
			if (r_data["thumb"] as Bitmap) {
				_box.pic.addChild(r_data["thumb"]);
			}
		}
		
		private function _setPhoto(r_data : Object) : void
		{
			if (r_data["thumb"] != null && r_data["thumb"] as Bitmap) {
				if(_components["UserInfoBox" + r_data["sit"]] != null){
					_components["UserInfoBox" + r_data["sit"]].pic.addChild(r_data["thumb"]);
				}
			}
		}
		
		//清除玩家操作提示框
		private function _hideUserInfo() : void
		{
			var _i : int = 1;
			var _obj : Object;
			for (; _i <= 9; _i++ ) {
				_obj = this._components["UserInfoBox" + _i] ;
				if (_obj != null) {
					_obj.hideStatus();
				}
			}
		}
			
		//移除玩家信息框
		private function _removeUserBox(r_sit: int) : void
		{		
			var _box = _components["UserInfoBox" + r_sit];
			if (_box != null) {				
				_box.parent.removeChild(_box);
				_components["UserInfoBox" + r_sit] = null;
			}
		}
					
		//設置座位上玩家數據
		private function _setUserSit(r_data : Object) : void
		{		
			var _class = _app.getClass("com.addcn.texas.view.components.UserInfoBox");
			if (_class == null) return ;	
			var _btn : Object;	
			var _sit : int = r_data["sit"];
			_addUserBox(r_data);				
			_removeButton(_sit);	
		}		
		
		//清除座位上的玩家
		private function _clearUserSit(r_data : Object) : void
		{
			var _sit  = int(r_data.sit);
			_removeUserBox(_sit);
			_addBuntton(_sit);
			if(this._my_sit == _sit){
				this._my_sit = 0;
			}
		}
		
		/**
		 * 设置玩家状态信息
		 */
		private function _showUserStatus(r_sit:int, r_msg : String) : void
		{
			var _obj : Object;
			var _user_box : Object 			
			_user_box = _components["UserInfoBox" + String(r_sit)];
			if(_user_box && r_msg!= null && r_msg!= ""){
				_user_box.showStatus(r_msg);			
			}
			
		}
		
		private function sitDownHanlder(evt : MouseEvent) :void
		{					
			var _sit : int = parseInt(evt.target.name);
			if (!Controller.getInstance().hasCommand(userOperationCommand)) {
				Controller.getInstance().addCommand(userOperationCommand);
			}
			sendNotification(userOperationCommand, { type:"ech", sit:_sit } );					
		}

		/**
		 * 显示游戏结果
		 * @param	r_data
		 */
		private function _showGameResult(r_data : Object) : void
		{
			var _sit : int;			
			var _info : Object;
			var _str : String;
			for (var k in r_data) 
			{
				_sit = int(k);
				_info = r_data[k];
				_str = _info["shape"] + " 赢了";
				_showUserStatus(_sit,_str);
			}
		}		
		
		override public function destroy():void 
		{		
			for each(var _val in _components) {
				if (_val != null && _val.parent) {
					_val.parent.removeChild(_val);					
				}
				_val = null;
			}		
			RemoveAllChildren.remove(_container as DisplayObject);			
			_components = null;
			_container = null;
		}
	}	
	
}
